package simpleHttpClient;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;

/**
 * The ConsoleUI's purpose is to output and input information to and from the user.
 * 
 * It accepts a Scanner and a PrintWriter in its constructor which will serve as the means of communicating with the user.
 * In addition, this class uses some functions from the javax.swing library when prompting the user for a directory location.
 * 
 * @author Rebecca Chandler
 * Scott Wendlandt
 * Li Weihan
 *
 */
public class ConsoleUI {

	/**
	 * This enum describes the possible actions the user may choose to take.
	 */
	public static enum Action{
		HTTP_REQUEST, CHANGE_FILE_LOC, DL_HTML_ELEMENTS, EXIT
	}

	/**
	 * The scanner from which the UI will read user input.
	 */
	private Scanner scanner;

	/**
	 * The writer with which the UI will output text to the user.
	 */
	private PrintWriter printWriter;

	/**
	 * Constructor.
	 * 
	 * @param s - the console's input scanner to read from.
	 * @param pw - the console's output writer to write to.
	 */
	public ConsoleUI(Scanner s, PrintWriter pw){
		this.scanner = s;
		this.printWriter = pw;
	}


	/**
	 * Prompts the user for the an action to take.
	 * 
	 * The user must choose one of the following:
	 * (1) New HTTP request
	 * (2) Change file-save location
	 * (3) Exit
	 * 
	 * @return The user's choice of action
	 */
	public Action promptNextAction() {
		Action action = null;

		while (action == null){
			printWriter.println("Please enter a number (1-4):\n" +
					"(1) New HTTP request\n" +
					"(2) Change file-save location\n" +
					"(3) Download html elements\n" +
					"(4) Exit");

			int selection = 0;
			try{
				selection = Integer.parseInt(scanner.nextLine().trim());
			}catch (java.lang.NumberFormatException e){
				displayResponse("Invalid entry.\n");
			}

			if(selection == 1){
				action = Action.HTTP_REQUEST;
			} else if (selection == 2){
				action = Action.CHANGE_FILE_LOC;
			} else if (selection == 3){
				action = Action.DL_HTML_ELEMENTS;
			}else if (selection == 4){
				action = Action.EXIT;
			}

		}
		return action;
	}

	/**
	 * Prompts the user to fill out an http request form consisting of two fields: host and object path.
	 * There is no check for misformed input.
	 * 
	 * @return A request object containing the user's entries.
	 */
	public Request promptRequest(){
		Request request = new Request();

		printWriter.println("\nPlease enter the domain name:");
		request.setHostName(scanner.nextLine().trim());

		printWriter.println("\nPlease enter the object path:");
		request.setObjectPathName(scanner.nextLine().trim());

		return request;
	}

	/**
	 * Uses the JFileChooser to prompt the user for a directory path.
	 * 
	 * @return File representing the selected directory.
	 */
	public File promptFileLocation(){
		JFileChooser dirChooser = new JFileChooser();
		dirChooser.setDialogTitle("Directory Chooser");
		dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
		dirChooser.setMultiSelectionEnabled(false);

		JFrame frame = new JFrame();
		frame.setVisible(true);

		int returnVal = JFileChooser.CANCEL_OPTION;
		do{
			returnVal = dirChooser.showDialog(frame.getContentPane(), "Select");
		}while(returnVal != JFileChooser.APPROVE_OPTION);

		frame.dispose();

		return dirChooser.getSelectedFile();
	}

	/**
	 * Uses the JFileChooser to prompt the user for a directory path.
	 * 
	 * @return File representing the selected directory.
	 */
	public File promptHtmlFile(){
		JFileChooser htmlChooser = new JFileChooser();
		htmlChooser.setDialogTitle("HTML File Chooser");
		htmlChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		htmlChooser.setMultiSelectionEnabled(false);
		htmlChooser.setFileFilter(new HtmlFileFilter());

		JFrame frame = new JFrame();
		frame.setVisible(true);

		int returnVal = JFileChooser.CANCEL_OPTION;
		do{
			returnVal = htmlChooser.showDialog(frame.getContentPane(), "Select");
		}while(returnVal != JFileChooser.APPROVE_OPTION);

		frame.dispose();

		return htmlChooser.getSelectedFile();
	}

	private class HtmlFileFilter extends FileFilter{

		@Override
		public boolean accept(File file) {
			boolean accepted = true;
			if(file.isFile()){
				String ext = file.getName();
				ext = ext.substring(ext.lastIndexOf('.')+1, ext.length());
				accepted = ext.equals("html");
			}
			return accepted;
		}

		@Override
		public String getDescription() {
			return "This filter accepts directories and html files";
		}

	}


	/**
	 * Output the response String through the ConsoleUI's PrintWriter
	 * 
	 * @param respose - the message to be outputted
	 */
	public void displayResponse(String respose){
		printWriter.println(respose);
	}

	/**
	 * Closes the Scanner and PrintWriter associated with this ConsoleUI
	 */
	public void close(){
		printWriter.close();
		scanner.close();
	}
}
